<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Article;
use App\Http\Model\Images;
use App\Http\Model\Logs;
use App\Http\Model\Star;
use App\Http\Model\User;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;

class UserController extends Controller
{
    /**
     * 用户列表
     * @return $this
     */
    public function userList(){
        check_auth();
        $endTime=$startTime=$username=$email=$status='';
        $allUser=User::orderBy('id','desc');
        if($search=Input::except('page')){
            $startTime=$search['start_time'];
            $endTime=$search['end_time'];
            $username=$search['username'];
            $email=$search['email'];
            $status=$search['status'];
            if(!empty($status)){
                $allUser=$allUser->where('status','=',$status);
            }
            if(!empty($username)){
                $allUser=$allUser->where('username','like',"%".$username."%");
            }
            if(!empty($email)){
                $allUser=$allUser->where('email','like',"%".$email."%");
            }
            if(!empty($startTime)){
                $allUser=$allUser->where('created_at','>',strtotime($startTime));
            }
            if(!empty($endTime)){
                $endTimer=strtotime($endTime)+3600*24;
                $allUser=$allUser->where('created_at','<',$endTimer);
            }
            $allUser=$allUser->paginate(7)->appends(['email' => $email,'start_time'=>$startTime,'end_time'=>$endTime,'username'=>$username,'status']);
        }else{
            $allUser=$allUser->paginate(7);
        }
        return view('admin.user.users',compact('allUser','endTime','startTime','username','email','status'))->with(['title'=>'用户管理']);
    }

    /**
     * 更改用户账号状态
     * @return array
     */
    public function updateUserStatus(){
        check_auth();
        if($input=Input::all()){
            return (bool)User::where('id',$input['id'])->update(['status'=>$input['status']])?['status'=>1,'msg'=>'修改完成']:['status'=>0,'msg'=>'修改失败'];
        }
    }

    /**
     * 审核作者
     * @return array
     */
    public function updateAuthorIs_author(){
        check_auth();
        Logs::create(['username'=>Session::get('admin')['username'],'event'=>'-更改了作者审核状态']);
        if($input=Input::all()){
            $userinfo=User::find($input['id']);
            if((bool)User::where('id',$input['id'])->update(['is_author'=>$input['is_author']])){
                if($input['is_author']==1){
                    $data = ['username'=>$userinfo['username'],'is_author'=>$input['is_author'],  'email'=>$userinfo['email'],'msg'=>'审核通过','url'=>url('admin/authorlogin')];
                }else if($input['is_author']==3){
                    $data = ['username'=>$userinfo['username'],'is_author'=>$input['is_author'], 'email'=>$userinfo['email'],'msg'=>'审核未通过','url'=>url('application')];
                }
                $flag = Mail::send('emails.authoremail',$data,function($message) use($data){
                    $message ->to($data['email'],$data['username'])->subject('申请作者审核结果');
                });
                return $flag?['status'=>1,'msg'=>'修改完成']:['status'=>0,'msg'=>'修改完成,但邮件未发送'];
            }else{
                return ['status'=>0,'msg'=>'修改失败'];
            }
        }
    }

    /**
     * 作者列表
     * @return $this
     */
    public function authorList(){
        check_auth();
        $endTime=$startTime=$username=$email=$is_author='';
        $allAuthor=User::where('sex','!=','')->where('role','!=','')->orderBy('id','desc');
        if($search=Input::except('page')){
            $startTime=$search['start_time'];
            $endTime=$search['end_time'];
            $username=$search['username'];
            $email=$search['email'];
            $is_author=$search['is_author'];
            if(!empty($is_author)){
                $allAuthor=$allAuthor->where('is_author','=',$is_author);
            }
            if(!empty($username)){
                $allAuthor=$allAuthor->where('username','like',"%".$username."%");
            }
            if(!empty($email)){
                $allAuthor=$allAuthor->where('email','like',"%".$email."%");
            }
            if(!empty($startTime)){
                $allAuthor=$allAuthor->where('created_at','>',strtotime($startTime));
            }
            if(!empty($endTime)){
                $endTimer=strtotime($endTime)+3600*24;
                $allAuthor=$allAuthor->where('created_at','<',$endTimer);
            }
            $allAuthor=$allAuthor->paginate(7)->appends(['email' => $email,'start_time'=>$startTime,'end_time'=>$endTime,'username'=>$username,'status']);
        }else{
            $allAuthor=$allAuthor->paginate(7);
        }
        return view('admin.user.authors',compact('allAuthor','endTime','startTime','username','email','is_author'))->with(['title'=>'作者管理']);
    }

    /**
     * 更多作者信息
     * @return mixed
     */
    public function authorMore(){
        check_auth();
        if($id=Input::except('_token')){
            return $this->userinfo($id);
        }
    }

    /**
     * 更多作者信息
     * @param $id 作者id
     * @return mixed
     */
    private function userinfo($id){
        $userinfo=DB::table('user')->where('id',$id)->get();
        switch($userinfo[0]->role){
            case 5:
                $userinfo[0]->role='文章&图片发布者';
                break;
            case 3:
                $userinfo[0]->role='图片发布者';
                break;
            case 2:
                $userinfo[0]->role='文章发布者';
                break;
        }
        switch($userinfo[0]->education){
            case 1:
                $userinfo[0]->education='小学';
                break;
            case 2:
                $userinfo[0]->education='初中';
                break;
            case 3:
                $userinfo[0]->education='高中';
                break;
            case 4:
                $userinfo[0]->education='大学';
                break;
            case 5:
                $userinfo[0]->education='大学以上';
                break;
        }
        $userinfo[0]->created_at=date('Y-m-d H:i:s',$userinfo[0]->created_at);
        $userinfo[0]->updated_at=date('Y-m-d H:i:s',$userinfo[0]->updated_at);
        $userinfo[0]->sex=$userinfo[0]->sex==1?'男':'女';
        $addrNum=explode('-',$userinfo[0]->address);
        $userinfo[0]->address='';
        foreach($addrNum as $addr){
            $addrName=DB::table('address')->where('id',$addr)->get();
            $userinfo[0]->address.=$addrName[0]->name.'-';
        }
        $userinfo[0]->address=rtrim($userinfo[0]->address,'-');
        return $userinfo;
    }

    /**
     * 作者详情
     * @return $this|array
     */
    public function authorInfo(){
        if($input=Input::except('_token')){
            $id=Session::get('admin')['id'];
            if(User::where('id',$id)->update($input)){
                return ['status'=>1,'msg'=>'更新成功'];
            }else{
                return ['status'=>0,'msg'=>'更新失败'];
            }
        }
        $userinfo=$this->userinfo(Session::get('admin')['id']);
        $logs=Logs::where('username',$userinfo[0]->username)->limit(50)->get()->all();
        $starP=Star::select('user.username','user.avatar','star.uid')->join('user','user.id','=','uid','left')->
        where(['star.type'=>3,'star.id'=>Session::get('admin')['id']])->get()->all();
        if(Session::get('admin')['role']==2 || Session::get('admin')['role']==5){
            $num['a']=Article::where('user_id',Session::get('admin')['id'])->count();
        }
        if(Session::get('admin')['role']==3 || Session::get('admin')['role']==5){
            $num['i']=Images::where('user_id',Session::get('admin')['id'])->count();
        }
        return view('admin.author.authorinfo',compact('userinfo','logs','starP','num'))->with(['title'=>'作者详情']);
    }
}
